package com.example.demo6.Controller;

import com.auth0.jwt.JWT;
import com.example.demo6.entity.LoginUser;
import com.example.demo6.utils.JWTUtils;
import com.example.demo6.utils.PassToken;
import com.example.demo6.utils.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.concurrent.TimeUnit;

@RestController
public class TestController {
    @Autowired
    RedisTemplate<String, Object> redisTemplate;
    @GetMapping("/test")
    public String test(){
        //读取redis中的数据
        String s=(String) redisTemplate.opsForValue().get("data");
        redisTemplate.opsForValue().set("age","21",20, TimeUnit.SECONDS);//保存
        return s;
    }
//    @PostMapping("/login")
//    public ServerResponse<String> login( @RequestBody LoginUser loginUser) {
//        //先判断验证码是否正确
//        //如何从redis取出保存的验证码
//        String storeCode = (String) redisTemplate.opsForValue().get("captcha_codes：" + loginUser.getUuid());
//        if (loginUser.getCode().equals(storeCode)) {
//            String id=loginUser.getUsername();
//            String pwd=loginUser.getPassword();
//            Map<String ,String> map=new HashMap<>();
//            map.put("username",id);
//            map.put("password",pwd);
//            if (id.equals("admin") && pwd.equals("admin")) {
//                String token = JWTUtils.getToken(map);
//                return ServerResponse.createBySuccess(200, token, "登陆成功");
//            }
//            return ServerResponse.createBySuccess(500, "", "账号或密码错误");
//        } else {
//            return ServerResponse.createBySuccess(500, "", "验证码错误");
//        }
//    }

    @PassToken
    @GetMapping("/getStudents")
    public ServerResponse<String> getStudents(){
        return ServerResponse.createBySuccess(200,"","查询成功");
    }
//    @GetMapping("/getInfo")
//    public ServerResponse<String> getInfo(HttpServletRequest request){
//String token=request.getHeader("Authorization");
//        System.out.println(token+" ");
//        //从token获得id
//        String id= JWT.decode(token.substring(token.lastIndexOf(" ")+1)).getClaim("username").asString();
////        String id= JWT.decode(token).getClaim("username").asString();
//        return ServerResponse.createBySuccess(200,id,"查询成功");
//    }
    @PassToken
    @GetMapping("/getBanners")
    public ServerResponse<List<String>> getBanners(){
        List<String> list=new ArrayList<>();
        list.add("/images/aa.jpg");list.add("/images/bb.jpeg");list.add("/images/cc.jpeg");list.add("/images/dd.jpeg");
        return ServerResponse.createBySuccess(200,list,"显示轮播图");
    }
}
